package com.sita.tingterest.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sita.bike.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class Navigationdeawerfragment extends Fragment {
    public static final String PREF_FILE_NAME = "testpref";
    private ActionBarDrawerToggle mDrawerToggle;
    public static final String KEY_USER_LEAREND_DRAWER = "user_learend_drawer";
    private DrawerLayout mdrawerLayout;
    private boolean mUserLeareneddrawer;
    private boolean mFromSaveInstans;
    private View containerView;

    public Navigationdeawerfragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserLeareneddrawer = Boolean.valueOf(readFromPreference(getContext(), KEY_USER_LEAREND_DRAWER, "false"));
        if (savedInstanceState != null) {
            mFromSaveInstans = true;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.ting_fragment_navigationdeawerfragment, container, false);
    }


    public void setUp(int fragment_id, DrawerLayout drawerLayout, final Toolbar toolbar) {
        containerView = getActivity().findViewById(fragment_id);
        this.mdrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!mUserLeareneddrawer) {
                    mUserLeareneddrawer = true;
                    saveToPreference(getActivity(), KEY_USER_LEAREND_DRAWER, mUserLeareneddrawer + "");

                }
                getActivity().invalidateOptionsMenu();

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                if (slideOffset < 0.5) {
                    toolbar.setAlpha(1 - slideOffset);
                }
            }
        };
        if (!mUserLeareneddrawer && !mFromSaveInstans) {
            drawerLayout.openDrawer(containerView);
        }
        mdrawerLayout.setDrawerListener(mDrawerToggle);
        mdrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

    }

    public static void saveToPreference(Context context, String preferenceName, String preferencValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(preferenceName, preferencValue);
        editor.apply();
    }

    public static String readFromPreference(Context context, String preferenceName, String preferencValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
        return sharedPreferences.getString(preferenceName, preferencValue);
    }
}
